package com.sirdc.modules.sys.web.index;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import net.huashitong.Indicators.databean.CompletionClassificationDatabean;
import net.huashitong.Indicators.filter.CompletionClassificationFilter;
import net.huashitong.Indicators.filter.IndicatorBreakdownFilter;
import net.huashitong.Indicators.service.IndicatorBreakdownService;
import net.huashitong.scheduling.databean.IndicatorSchedulingDatabean;
import net.huashitong.scheduling.entity.AreaNumber;
import net.huashitong.scheduling.entity.IndicatorScheduling;
import net.huashitong.scheduling.filter.IndicatorSchedulingFilter;
import net.huashitong.scheduling.service.AreaNumberService;
import net.huashitong.scheduling.service.IndicatorSchedulingService;
import net.huashitong.scheduling.service.PreemptiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/index/indicatorScheduling/")
public class IndexIndicatorSchedulingController  extends JsonBaseController {
    @Override
    protected String getView(String arg0) {
        return null;
    }
    @Autowired
    private IndicatorSchedulingService schedulingService;
    @Autowired
    private IndicatorBreakdownService indicatorBreakdownService;
    @Autowired
    private AreaNumberService areaNumberService;
    @Autowired
    private PreemptiveService preemptiveService;

    /************************************地区针对性比较*******************************************************
    /**
     * 获取血拼排名列表
     * @return
     */
    @ResponseBody
    @RequestMapping("/queryAreaNumberLis")
    public Message queryAreaNumberList(){
        List<AreaNumber> anList=areaNumberService.query();
        List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
        for(AreaNumber am:anList){
            if(!"YT".equals(am.getNumber())){
                Map<String, Object> map2 = new HashMap<String, Object>();
                map2.put("area", am.getName());
                map2.put("number", am.getNumber()+am.getRanking());
                list.add(map2);
            }
        }
        return coverMessage("200", "获取成功", list);
    }

    /**
     * 查询血拼数据
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping("/queryShopping")
    public Message queryShopping(@RequestBody IndicatorBreakdownFilter filter){
        List<String> industryNames = new ArrayList<String>();
        industryNames.add("地区生产总值");
        industryNames.add("第一产业增加值");
        industryNames.add("第二产业增加值");
        industryNames.add("第三产业增加值");

        filter.setIndustryNames(industryNames);
        //filter = AreaNameUtils.getAreaName(filter);
        List<Map<String, Object>> maps = schedulingService.queryShopping(filter);
        return coverMessage("200", "数据获取成功",maps.get(1));
    }

    /*********************************************GDP增长态势图****************************************/
    /**
     * 前六末六数据
     */
    @ResponseBody
    @RequestMapping("/queryPreemptive")
    public Message queryPreemptive(@RequestBody CompletionClassificationFilter filter){
        filter.setCategory("争先进位");
        List<CompletionClassificationDatabean> classificationDatabeans = preemptiveService.queryPreemptive(filter);
        return coverMessage("200", "数据获取成功", classificationDatabeans);
    }
    /****************************************预测模块*******************************************************/
    /**
     * 查询指标数据
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping("/queryValue")
    public Message queryValue(@RequestBody IndicatorSchedulingFilter filter){
        if("GDP".equals(filter.getIndicatorsCategory())){
            filter.setMonth("09");
        }
        Map<String, Object> map = new HashMap<String, Object>();
        List<Map<String, Object>> maps = new ArrayList<Map<String,Object>>();
        List<IndicatorScheduling> lists = schedulingService.queryByCategory(filter);
        List<IndicatorSchedulingDatabean> databeans = new ArrayList<IndicatorSchedulingDatabean>();
        for (IndicatorScheduling indicatorScheduling : lists) {
            IndicatorSchedulingDatabean databean = new IndicatorSchedulingDatabean();
            databean.setIndicatorName(indicatorScheduling.getIndicatorName());
            Map<String, Object> map2 = new HashMap<String, Object>();
            databean.setRatio(indicatorScheduling.getRatio());
            if(indicatorScheduling.getIndicatorName().equals(filter.getIndicatorsCategory())){
                map .put("name",indicatorScheduling.getIndicatorName());
                map .put("value",indicatorScheduling.getRatio());
                map2.put("name",indicatorScheduling.getIndicatorName());
                map2.put("value",indicatorScheduling.getRatio());
            }else{
                map2.put("name",indicatorScheduling.getIndicatorName());
                map2.put("value",indicatorScheduling.getRatio());

            }
            maps.add(map2);

        }
        map.put("list",maps);
        return coverMessage("200", "获取成功",map);
    }

    /**
     * 处理调度数据
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping("/detailIndicator")
    public Message detailIndicator(@RequestBody IndicatorSchedulingFilter filter){
        List<IndicatorScheduling> indicatorSchedulings = filter.getLists();
        filter.setMonth("06");
        List<IndicatorScheduling> lists = schedulingService.queryByCategory(filter);
        for (IndicatorScheduling indicatorScheduling : indicatorSchedulings) {
            for (IndicatorScheduling indicatorSchedul : lists) {
                if(indicatorScheduling.getIndicatorName().equals(indicatorSchedul.getIndicatorName())){
                    Double doubles = Double.parseDouble(indicatorScheduling.getRatio());
                    String st = "";
                    if(doubles != 0){
                        DecimalFormat df=new DecimalFormat(".##");
                        st=df.format(doubles);
                        indicatorSchedul.setRatio(st);
                    }else {
                        indicatorSchedul.setRatio(indicatorScheduling.getRatio());
                        st = indicatorSchedul.getRatio();
                    }
                    Double double1 = Double.parseDouble(st);
                    Double double2 = Double.parseDouble(indicatorSchedul.getLastYearValue());
                    if("0".equals(indicatorSchedul.getRatio())){
                        indicatorSchedul.setYearPredict("0");
                    }else {
                        indicatorSchedul.setYearPredict(Double.toString(double2*(100+double1)/100));
                    }
                }
            }
        }
        if("0".equals(lists.get(0).getRatio())){
            Double double3 = new Double(0);
            for (int j = 0; j < lists.size(); j++) {
                if(j == 0){
                    continue;
                }
                double3 += Double.parseDouble(lists.get(j).getYearPredict());
            }

            lists.get(0).setYearPredict(Double.toString(double3));
            Double double4 = Double.parseDouble(lists.get(0).getLastYearValue());
            DecimalFormat df=new DecimalFormat(".##");
            String st=df.format((double3-double4)/double4*100);
            lists.get(0).setRatio(st);
        }else {
            Double double3 = Double.parseDouble(lists.get(0).getYearPredict());
            Double double4 = new Double(0);
            int k = 0;
            for (int i = 0; i < lists.size(); i++) {
                if(i == 0){
                    continue;
                }else if("0".equals(lists.get(i).getRatio())){
                    k = i;
                }else {
                    double4 += Double.parseDouble(lists.get(i).getYearPredict());
                }
            }
            lists.get(k).setYearPredict(Double.toString(double3-double4));
            Double double5 = Double.parseDouble(lists.get(k).getLastYearValue());
            DecimalFormat df=new DecimalFormat(".##");
            String st=df.format(((double3-double4-double5)/double5*100));
            lists.get(k).setRatio(st);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        List<Map<String, Object>> maps = new ArrayList<Map<String,Object>>();
        List<IndicatorSchedulingDatabean> databeans = new ArrayList<IndicatorSchedulingDatabean>();
        for (IndicatorScheduling indicatorScheduling : lists) {
            Map<String, Object> map2 = new HashMap<String, Object>();
            IndicatorSchedulingDatabean databean = new IndicatorSchedulingDatabean();
            databean.setIndicatorName(indicatorScheduling.getIndicatorName());
            databean.setRatio(indicatorScheduling.getRatio());
            if(indicatorScheduling.getIndicatorName().equals(filter.getIndicatorsCategory())){
                map .put("name",indicatorScheduling.getIndicatorName());
                map .put("value",indicatorScheduling.getRatio());
                map2.put("name",indicatorScheduling.getIndicatorName());
                map2.put("value",indicatorScheduling.getRatio());
                maps.add(0,map2);
            }else{
                map2.put("name",indicatorScheduling.getIndicatorName());
                map2.put("value",indicatorScheduling.getRatio());
                maps.add(map2);
            }

        }
        map.put("list",maps);
        return coverMessage("200", "获取成功",map);
    }
}
